1915G - Bicycles - CodeForces Solution


dp graphs greedy implementation shortest paths sortings

Please click on ads to support us..

C++ Code:

#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;
#define ordered_set tree<int, null_type,less<int>, rb_tree_tag,tree_order_statistics_node_update>
#define mod 1000000007
#define int long long


int32_t main (){
ios_base::sync_with_stdio(false);
cin.tie(NULL);
    int tt;
  cin >> tt;
  while (tt--) {
    int n, m;
    cin >> n >> m;
    vector<vector<pair<int, int>>> g(n);
    for (int i = 0; i < m; i++) {
      int a, b, c;
      cin >> a >> b >> c;
      --a; --b;
      g[a].emplace_back(b, c);
      g[b].emplace_back(a, c);
    }
    vector<int> w(n);
    for (int i = 0; i < n; i++) {
      cin >> w[i];
    }
    vector<vector<int>> dist(n, vector<int>(n, numeric_limits<int>::max()));
    priority_queue<array<int, 3>, vector<array<int, 3>>, greater<array<int, 3>>> s;
    dist[0][0] = 0;
    s.push({dist[0][0], 0, 0});
    while (!s.empty()) {
      int expected = s.top()[0];
      int i = s.top()[1];
      int j = s.top()[2];
      s.pop();
      if (dist[i][j] != expected) {
        continue;
      }
      if (i == n - 1) {
        cout << dist[i][j] << '\n';
        break;
      }
      for (auto& p : g[i]) {
        int ti = p.first;
        int tj = (w[j] < w[p.first] ? j : p.first);
        if (dist[i][j] + p.second * w[j] < dist[ti][tj]) {
          dist[ti][tj] = dist[i][j] + p.second * w[j];
          s.push({dist[ti][tj], ti, tj});
        }
      }
    }
  }
  return 0;
}


Comments

Submit
0 Comments
More Questions

102B - Sum of Digits
707A - Brain's Photos
1331B - Limericks
305B - Continued Fractions
1165B - Polycarp Training
1646C - Factorials and Powers of Two
596A - Wilbur and Swimming Pool
1462B - Last Year's Substring
1608B - Build the Permutation
1505A - Is it rated - 2
169A - Chores
765A - Neverending competitions
1303A - Erasing Zeroes
1005B - Delete from the Left
94A - Restoring Password
1529B - Sifid and Strange Subsequences
1455C - Ping-pong
1644C - Increase Subarray Sums
1433A - Boring Apartments
1428B - Belted Rooms
519B - A and B and Compilation Errors
1152B - Neko Performs Cat Furrier Transform
1411A - In-game Chat
119A - Epic Game
703A - Mishka and Game
1504C - Balance the Bits
988A - Diverse Team
1312B - Bogosort
1616B - Mirror in the String
1660C - Get an Even String